package com.rabbitmq.extension.listener;


import com.rabbitmq.client.Channel;
import com.rabbitmq.extension.constant.Constants;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

//@Component
public class Consumer {
    @RabbitListener(queues = Constants.RETRY_QUEUE)
    public void ListenerQueue(Message message, Channel channel) throws IOException, InterruptedException {

        try{
            System.out.printf("接收到信息: %s, deliveryTag: %d%n",new String(message.getBody(),"UTF-8"), message.getMessageProperties().getDeliveryTag());
            int num=3/0;
            System.out.println("处理完成");
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
//            System.out.println("处理失败");
            Thread.sleep(1000);//防止出现异常->重发->又异常->又重发
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }

    }

}
